<?php

/**
 * MfileModel.php 注释
 * 
 * @package 
 * @subpackage 
 * @author rendong01 rendong237@126.com
 * 
 * : tool.php 28 2012-12-08 06:14:13Z rendong237@gmail.com $
 */
class MfileModel{
	/**
	 * 数据库对象
	 *
	 * @var Mysql
	 */
	private $db;
	
	/**
	 * 构造方法
	 *
	 * @param Mysql $db
	 */
	public function __construct($db)
	{
		$this->db = $db;
	}
	
	/**
	 * 添加
	 */
	public function add( array $value )
	{
		$v = array();
//		$v["fid"] = (int)$value["fid"];
		$v["mid"] = (int)$value["mid"];
		$v["uid"] = (int)$value["uid"];
		$v["uuid"] = $value["uuid"];
		$v["filename"] = $value["filename"];
		
		$this->db->insert("mfile", $v);
	}
	
	/**
	 * 修改
	 */
	public function change($fid, array $value)
	{
		$v = array();
		isset($value["mid"]) && $v["mid"] = (int)$value["mid"];
		isset($value["uid"]) && $v["uid"] = (int)$value["uid"];
		isset($value["uuid"]) && $v["uuid"] = $value["uuid"];
		isset($value["filename"]) && $v["filename"] = $value["filename"];

		$this->db->update("mfile", $v, 'fid=' . (int)$fid);
	}
	
	/**
	 * 删除
	 */
	public function del($fid)
	{
		$this->db->delete("mfile", 'fid=' .  (int)$fid);
	}
	
	/**
	 * 读取列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function fetchAll($nowp, $per_page)
	{
		$where = '';		
		$totalNum = $this->db->fetchOne ('select count(*) from mfile ' . $where);
        
        require_once APP . '/application/models/Util.php';
        
        $pageInfo = Util::page ($totalNum, $per_page, $nowp);
        $start    = $pageInfo ['start'];
        $list     = $this->db->fetchAll ('select * from mfile' . $where .' order by fid desc limit ' . $start . ',' . $per_page);
        
        return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	/**
	 * 显示
	 */
	public function show($fid)
	{
		return $this->db->fetchRow("select * from `mfile` where fid=\"?\"", array($fid));
	}
}